package www.houdoudou.com.supplier;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

public class UpLoadServlet extends HttpServlet {
	private static final long serialVersionUID = 4L;
	public void doPost(HttpServletRequest request, HttpServletResponse response)  
	           throws ServletException, IOException {  
		 
		       response.setCharacterEncoding("UTF-8");
		       PrintWriter out = response.getWriter();
		       
	           String temp=request.getSession().getServletContext().getRealPath("/")+"temp";   //临时目录
	           System.out.println("temp="+temp);
	        //原来写法   String loadpath=request.getSession().getServletContext().getRealPath("/")+"image"; //上传文件存放目录
	           //现在的写法，，获取根目录，文件存放位置由文件名决定，文件名由添加菜品时目录生成程序段决定。，
	           String loadpath=request.getSession().getServletContext().getRealPath("/");
	           System.out.println("loadpath="+loadpath);
	           DiskFileUpload fu =new DiskFileUpload();
	           fu.setSizeMax(1*1024*1024);   // 设置允许用户上传文件大小,单位:字节
	           fu.setSizeThreshold(4096);   // 设置最多只允许在内存中存储的数据,单位:字节
	           fu.setRepositoryPath(temp); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
	           System.out.println("Headler Content-Type="+request.getHeader("Content-Type"));  
	           //开始读取上传信息
	            //int index=0;
	           List fileItems =null;
	                
	                        
	                                try {
	                                        fileItems = fu.parseRequest(request);
	                                         System.out.println("fileItems="+fileItems);
	                                } catch (Exception e) {
	                                        e.printStackTrace();
	                                        out.println("解析数据时出现如下问题："); 
	                                        return;
	                                }
	                        
	                
	           Iterator iter = fileItems.iterator(); // 依次处理每个上传的文件
	while (iter.hasNext())
	           {
	               FileItem item = (FileItem)iter.next();// 忽略其他不是文件域的所有表单信息
	if (!item.isFormField())
	               {  
		               // 文件名处理
	                   String name = item.getName();//获取上传文件名,包括路径
	                   System.out.println("name--"+name);
	       
	                   long size = item.getSize();
	                   if((name==null||name.equals("")) && size==0)
	                   {   out.println("ERROR-FILE");
	                       System.out.println("ERROR-FILE");
	                	   continue;
	                   }
	               
	                    
	                    /*------  http://192.168.1.103:8080/Houdou/image/13581617523/15520521958/9.jpg -----------*/
	                    
	                                                  
	                    
	                   //重写名称和目录的处理
	                   String[] nameparse=name.split("\\/");
                       for(int i=0;i<nameparse.length;i++){
                    	  System.out.println("--"+i+"---"+nameparse[i]); 
                       } 
                       
	                   if (nameparse.length!=8){
	                	   out.println("ERROR-NAME"); 
	                	   System. out.println("ERROR-NAME"); 
	                	   continue;
	                   }
	                   //一级目录 获取根目录时已经加了/
	                   loadpath=loadpath+nameparse[4];
	                   File fUploadDir = new File(loadpath); 
	                   if(!fUploadDir.exists()) 
	                   { 
	                        if(!fUploadDir.mkdir()) 
	                        { 
	                            out.println("ERROR-1FOLDER"); 
	                            System.out.println("ERROR-1FOLDER"); 
	                            continue;
	                        } 
	                    }  
	                 //二级目录
	                   loadpath=loadpath+"/"+nameparse[5];
	                   File fUploadDir2 = new File(loadpath); 
	                   if(!fUploadDir2.exists()) 
	                   { 
	                        if(!fUploadDir2.mkdir()) 
	                        { 
	                            out.println("ERROR-2FOLDER"); 
	                            System.out.println("ERROR-2FOLDER"); 
	                            continue;
	                        } 
	                    }  
	                   //三级目录
	                   loadpath=loadpath+"/"+nameparse[6];
	                   File fUploadDir3 = new File(loadpath); 
	                   if(!fUploadDir3.exists()) 
	                   { 
	                        if(!fUploadDir3.mkdir()) 
	                        { 
	                            out.println("ERROR-3FOLDER"); 
	                            System.out.println("ERROR-3FOLDER"); 
	                            continue;
	                        } 
	                    }  
	                   // 名称
	                   name=nameparse[7];
	                   ///////////////////////////////////////////////
	                   //文件存储
	                   try {   File fNew=new File(loadpath, name);
	                  
	                         item.write(fNew);
	                         out.println("OK");
	                         System.out.println("OK");
	                   } catch (Exception e) {
	                                        // TODO Auto-generated catch block
	                                        e.printStackTrace();
	                                        out.println("ERROR-SAVE");
	                                        System.out.println("ERROR-SAVE");
	                                }finally{
	                                	item.delete();
	                                }
	                  
	                  
	               }
	               else//取出不是文件域的所有表单信息
	               {    
	            	   String fieldvalue= item.getString("utf-8");
	                  // String fieldvalue = item.getString();
	            //如果包含中文应写为：(转为UTF-8编码)
	                 //  String fieldvalue = new String(item.getString().getBytes(),"UTF-8");
	                   System.out.println("Supplier_Telnum="+fieldvalue);
	               }
	           } // while(iter.hasNext())
	        //最后关闭
	       out.close();
	    }  

}
